var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/index.ts
var src_exports = {};
__export(src_exports, {
  default: () => src_default
});
module.exports = __toCommonJS(src_exports);
var import_withTmpPath = require("./utils/withTmpPath");
var src_default = (api) => {
  api.describe({
    key: "drsMaster",
    config: {
      default: {
        enablePatchMenus: true
      },
      schema(Joi) {
        return Joi.alternatives().try(
          Joi.object({
            /** 是否启用 */
            enable: Joi.boolean(),
            /** 本地调试子应用，开发和生产环境均有效 */
            localApps: Joi.array(),
            /** 注册本地应用时是否追加菜单 */
            enablePatchMenus: Joi.boolean()
          }),
          Joi.boolean().invalid(true)
        );
      }
    },
    enableBy: (opts) => {
      var _a;
      return ((_a = opts.userConfig.drsMaster) == null ? void 0 : _a.enable) !== false;
    }
  });
  api.modifyConfig((memo) => {
    return {
      ...memo,
      qiankun: {
        master: {}
      }
    };
  });
  api.onGenerateFiles(() => {
    api.writeTmpFile({
      path: "NoAuthPage.tsx",
      content: `import React from 'react';
import { useModel } from 'umi';
import { DrsNoAuth } from '@szzj/drs-share';
import { type IDrsState } from 'umi';

export default function NoAuthPage() {
  const { userInfo = {} } = useModel('@@drsModel') as IDrsState;

  return (
    <DrsNoAuth userInfo={userInfo} />
  );
}      
`
    });
    api.writeTmpFile({
      path: "NoRolePage.tsx",
      content: `import React from 'react';
import { DrsNoRole } from '@szzj/drs-share';

export default function NoRolePage() {
  return (
    <DrsNoRole />
  );
}      
`
    });
    const { localApps = [], enablePatchMenus = true } = api.config.drsMaster ?? {};
    api.writeTmpFile({
      path: "runtime.tsx",
      content: `import React from 'react';
import ReactDOM from 'react-dom';
import { createRoot } from 'react-dom/client';
import { history } from 'umi';
import { getQiankunConfig, patchSlaveMenus } from '@szzj/drs-share-master';

window.drsAppCode = '${api.pkg.name}';

/**
 * 通过 window 变量共享 history 主子应用跳转方法 
 */
window.drsHistory = history;

/**
 * 通过 event 事件跳转错误页面
 */
window.addEventListener('403', () => history.push('/403'));
window.addEventListener('404', () => history.push('/404'));
window.addEventListener('500', () => history.push('/500'));
window.addEventListener('norole', () => history.push('/norole'));
window.addEventListener('noauth', () => history.push('/noauth'));
      
/**
 * @fixme
 * 如果没有加载 React，手动赋值，兼容 react@16
 */
if (!window.React) {
  window.React = React;
  window.ReactDOM = {
    ...ReactDOM,
    render(element, root) {
      return createRoot(root).render(element);
    },
  };
}

const apps = (${JSON.stringify(localApps, null, 2)} ?? []).map(app => {
  // 相对路径
  if (app.entry?.indexOf('/') === 0 && app.entry?.indexOf('//') !== 0) 
    return {
      ...app,
      entry: location.origin + app.entry,
    };

  // 绝对路径
  return app;
});

export const qiankun = () => {
  return getQiankunConfig(apps);
};

${enablePatchMenus ? `export const patchMenus = ({ menus }: { menus: any[] }) => {
  return patchSlaveMenus({ menus, apps })
};` : ""}
`
    });
  });
  api.addRuntimePlugin(() => {
    return [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })];
  });
  api.modifyRoutes((memo) => {
    memo["noauth"] = {
      id: "noauth",
      path: "/noauth",
      absPath: "/noauth",
      parentId: "drs-layout",
      file: (0, import_withTmpPath.withTmpPath)({ api, path: "NoAuthPage.tsx" }),
      exact: true
    };
    memo["norole"] = {
      id: "norole",
      path: "/norole",
      absPath: "/norole",
      parentId: "drs-layout",
      file: (0, import_withTmpPath.withTmpPath)({ api, path: "NoRolePage.tsx" }),
      exact: true
    };
    return memo;
  });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
